<?php

class Baike {
	private $message = "";
    function Baike() {
    	
    	
    }
    public function run() {
    	$p ='/@baike\s+(.*)/';
    	preg_match($p, $_POST[Message], $m);
    	//print_r($m);
    	$this->getBaikeCard($m[1]);
    }
    
    public function getBaikeCard($name) {
    	$mess = "";
    	$url = "http://baike.baidu.com/searchword/?word=".rawurlencode($name)."&pic=1&sug=1&ie=utf-8&enc=utf-8&oq=bai&rsp=%5Bobject+Object%5D";
		$url = "http://baike.baidu.com/search/word?word=".rawurlencode($name)."&pic=1&sug=1";
		echo $url;
    	
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);

		// 不需要页面内容

		curl_setopt($ch, CURLOPT_NOBODY, 1);

		// 不直接输出

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		// 返回最后的Location

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

		curl_exec($ch);

		$info = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);

		curl_close($ch);

		echo '真实url为：'.$info;
		
		$url = $info;
		$do = new CurlUtil($url);
		$do->init();
		$body = $do->getBody();
		echo $body ;
		return;
		$output = file_get_contents($url);
    	//$output = mb_convert_encoding($output, 'UTF-8', 'GB2312');
		echo $output ;
    	$p = '/<img class=\"card-image editorImg log-set-param\" alt=\"(.*?)\" src=\"(.*?)\"/is'; 
    	preg_match($p, $output, $m);
    	if($m) {
    		$mess = $mess."[img]".$m[2]."[/img]"."\n";
    	}
    	//print_r($m);
    	$p = '/<div class=\"card-summary-content\">(.*?)<\/div>/is';
    	preg_match($p, $output, $n);
    	if($n) {
    		$this->message = strip_tags( $mess.$n[1]);
    	}else{
    		$this->message ="没有找到相关词条-。-";
    	}
    }
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}    
}
?>